package com.linux.huhx.thread3.threadState_1;

import com.linux.huhx.thread2.ThreadB;

/**
 * @Author: huhx
 * @Date: 2017-10-31 下午 3:11
 * STATE: WAITING
 */
public class StateRun4 {
    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();
        Thread.sleep(1000);
        System.out.println("In main thread state: " + myThread.getState()); // WAITING

        new MyThreadB().start();
    }

    static class Lock {
        public static final Byte lock = new Byte("0");
    }

    static class MyThread extends Thread {
        @Override
        public void run() {
            try {
                synchronized (Lock.lock) {
                    System.out.println("before wait: " + Thread.currentThread().getState()); // RUNNABLE
                    Lock.lock.wait();
                    System.out.println("after wait: " + Thread.currentThread().getState()); // RUNNABLE
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class MyThreadB extends Thread {
        @Override
        public void run() {
            synchronized (Lock.lock) {
                Lock.lock.notify();
            }
        }
    }
}
/*
before wait: RUNNABLE
In main thread state: WAITING
after wait: RUNNABLE
 */